﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Text.RegularExpressions;

using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using ThoughtWorks.QRCode.Codec.Util;  

namespace MicroTaskTest
{
    public partial class QRCode : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int a = 1;
        }
       
        /// <summary>  
        /// 10进制或16进制转换为中文  
        /// </summary>  
        /// <param name="name">要转换的字符串</param>  
        /// <param name="fromBase">进制（10或16）</param>  
        /// <returns></returns>  
        public string ConverToGB(string text, int fromBase)
        {
            string value = text;
            MatchCollection mc;
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            switch (fromBase)
            {
                case 10:

                    MatchCollection mc1 = Regex.Matches(text, @"&#([\d]{5})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    foreach (Match _v in mc1)
                    {
                        string w = _v.Value.Substring(2);
                        w = Convert.ToString(int.Parse(w), 16);
                        byte[] c = new byte[2];
                        string ss = w.Substring(0, 2);
                        int c1 = Convert.ToInt32(w.Substring(0, 2), 16);
                        int c2 = Convert.ToInt32(w.Substring(2), 16);
                        c[0] = (byte)c2;
                        c[1] = (byte)c1;
                        sb.Append(Encoding.Unicode.GetString(c));
                    }
                    value = sb.ToString();

                    break;
                case 16:
                    mc = Regex.Matches(text, @"\\u([\w]{2})([\w]{2})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    if (mc != null && mc.Count > 0)
                    {

                        foreach (Match m2 in mc)
                        {
                            string v = m2.Value;
                            string w = v.Substring(2);
                            byte[] c = new byte[2];
                            int c1 = Convert.ToInt32(w.Substring(0, 2), 16);
                            int c2 = Convert.ToInt32(w.Substring(2), 16);
                            c[0] = (byte)c2;
                            c[1] = (byte)c1;
                            sb.Append(Encoding.Unicode.GetString(c));
                        }
                        value = sb.ToString();
                    }
                    break;
            }
            return value;
        }

        protected void btnGo_Click(object sender, EventArgs e)
        {
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            qrCodeEncoder.QRCodeScale = 4; //size
            qrCodeEncoder.QRCodeVersion = 7;//1-40
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//level


            //String data = ConverToGB("htt://www.baidu.com", 16);
            string data = "http://www.baidu.com";
            using (var image = qrCodeEncoder.Encode(data))
            {
                string filename = Guid.NewGuid().ToString() + ".jpg";
                string filepath = Server.MapPath(@".") + "\\" + filename;
                using (System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
                {
                    image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
            }
        }
    }
}